<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Assessment extends Model
{
    //
    protected $fillable = ['agency_id', 'user_id', 'child_id', 'age', 'agedevelop', 'tool_id', 'assess_time', 'stage'];

    const STAGE = [0=>'训练前期', 1=>'训练中期', 2=>'训练后期'];

    protected $dates = [];

    public function agency(){
        return $this->belongsTo(Agency::class);
    }
    public function user(){
        return $this->belongsTo(User::class);
    }

    public function child(){
        return $this->belongsTo(Child::class);
    }

    public static function getStage(){
        return self::STAGE;
    }

    public function tool(){
        return $this->belongsTo(Tool::class);
    }
    /*

 * 拼接查询条件
 *      */
    public static function selectQuery($data){
        return function ($query) use ($data) {
            if(isset($data['agency_id']) && $data['agency_id'] > 0){
                $query->where('agency_id', $data['agency_id']);
            }
            if(isset($data['user_id']) && $data['user_id'] > 0){
                $query->where('user_id', $data['user_id']);
            }
            if(isset($data['tool_id']) && $data['tool_id'] > 0){
                $query->where('tool_id', $data['tool_id']);
            }
            if (isset($data['child_name']) && !empty($data['child_name'])) {
                $query->where(function($s) use ($data){
                    $s->whereHas('child', function($item) use ($data){
                        $item->where('name', 'like',  '%' . $data['child_name'] . '%');
                    });
                });
            }
        };
    }
}
